package Queue;

class MyCircularQueue {
    private int[] elem;
    private int rear;
    private int front;

    public MyCircularQueue(int k) {
        this.elem = new int[k + 1];
        this.front = 0;
        this.rear = 0;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        if((rear + 1) % elem.length == front) {
            return true;
        }
        return false;
    }

    public boolean inDequeue(int val) {//入循环队列
        if(isFull()) {//如果是满的
            return false;
        }
        elem[rear] = val;
        rear = (rear + 1) % elem.length;
        return true;
    }

    public boolean outDequeue() {
        if(isEmpty()) {//空的不能出
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    public int Front() {//得到队头元素
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    public int Rear() {//得到队尾元素
        if(isEmpty()) {
            return -1;
        }
        int  tail = (rear - 1 + elem.length) % elem.length;
        return elem[tail];
    }
}
